package com.cl.unipay4j.ali.pay;

import com.alibaba.fastjson.JSON;
import com.alipay.api.AlipayApiException;
import com.alipay.api.request.AlipayTradeCreateRequest;
import com.alipay.api.request.AlipayTradePrecreateRequest;
import com.alipay.api.response.AlipayTradeCreateResponse;
import com.alipay.api.response.AlipayTradePrecreateResponse;
import com.cl.unipay4j.core.IPayConfig;
import com.cl.unipay4j.core.Result;
import lombok.extern.slf4j.Slf4j;

import java.util.Map;

/**
 * 支付宝扫码支付
 *
 * 当面付-扫码支付-统一统一收单交易创建接口
 *
 * <p>场景</p>
 * 商户通过该接口进行交易的创建下单
 *
 * @author siashan
 * @since v1.0.1
 **/
@Slf4j
public class AliPayFace extends AliPay {


    /**
     * 销售产品码，商家和支付宝签约的产品码
     */
    private static final String PRODUCT_CODE = "FACE_TO_FACE_PAYMENT";


    public AliPayFace(IPayConfig aliPaySet) {
        super(aliPaySet);
    }

    public AliPayFace() {
        super();
    }


    /**
     * 方法描述: 支付
     *
     * @param params  支付参数
     * @return 支付结果
     */
    @Override
    public Result executePay(Map<String, Object> params) throws AlipayApiException {
        AlipayTradeCreateRequest alipayRequest = new AlipayTradeCreateRequest();
        alipayRequest.setNotifyUrl(payConfig.getNotifyUrl());
        params.put("product_code", PRODUCT_CODE);
        alipayRequest.setBizContent(JSON.toJSONString(params));
        AlipayTradeCreateResponse r = this.alipayClient.execute(alipayRequest);
        if (r.isSuccess()) {
            return Result.returnSuccess(r.getTradeNo());
        } else {
            return Result.returnFail(r.getSubCode(), r.getSubMsg());
        }
    }

    /**
     * 获取交易类型
     *
     * @return  交易类型
     */
    @Override
    public AliTradeTypeEnum getTradeType() {
        return AliTradeTypeEnum.NATIVE;
    }

    @Override
    public boolean needBuyerId() {
        return true;
    }
}
